//
//  GuiderCardCell.m
//  ImGuider
//
//  Created by 王鹏宇 on 2017/4/19.
//  Copyright © 2017年 imguider. All rights reserved.
//

#import "GuiderCardCell.h"
#import "GuiderCardScenicCell.h"
#import "StarRateView.h"
#import "GuiderIconView.h"
@interface GuiderCardCell()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>


@property (weak, nonatomic) IBOutlet UILabel *nameLabel;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *AuthenticationLabelH;

@property (weak, nonatomic) IBOutlet UILabel *introduceLabel;

@property (weak, nonatomic) IBOutlet UILabel *viewCountLabel;

@property (weak, nonatomic) IBOutlet UILabel *visitCountLabel;

@property (weak, nonatomic) IBOutlet UILabel *favourCountLabel;

@property (weak, nonatomic) IBOutlet UICollectionView *ScenicCollectionView;

@property (weak, nonatomic) IBOutlet UIView *bgView;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *introduceLabelWidth;

@property (weak, nonatomic) IBOutlet UILabel *guiderTagLabel;

@property (weak, nonatomic) IBOutlet GuiderIconView *guiderHeaderView;

@property (weak, nonatomic) IBOutlet UIImageView *collectMarImageView;

@end

@implementation GuiderCardCell

- (void)awakeFromNib {
    
    [super awakeFromNib];
    
    self.introduceLabelWidth.constant =  SCREEN_WIDTH - 130;
    self.layer.masksToBounds = NO;
    
    self.layer.shadowOpacity = 0.5f;
    
    self.layer.shadowRadius = 3.0f;
    
    self.layer.shadowOffset = CGSizeMake(0,0);
    self.layer.shadowColor = [UIColor blackColor].CGColor;
    
    //设置缓存
    
    self.layer.shouldRasterize = YES;
    
    //设置抗锯齿边缘
    
    self.layer.rasterizationScale = [UIScreen mainScreen].scale;
    
    self.collectMarImageView.hidden = YES;

    _ScenicCollectionView.delegate = self;
    _ScenicCollectionView.dataSource = self;
    [_ScenicCollectionView registerNib:[UINib nibWithNibName:@"GuiderCardScenicCell" bundle:nil] forCellWithReuseIdentifier:@"GuiderCardScenicCell"];
    
    self.guiderTagLabel.textColor = kThemeRedColor;
    self.nameLabel.textColor = kTitleColor;
    self.introduceLabel.textColor = kContentColor;
    self.viewCountLabel.textColor = kContentColor;
    self.favourCountLabel.textColor = kContentColor;
    self.visitCountLabel.textColor = kContentColor;
}

//- (void)viewWillLayoutSubviews {
//   // [super]
//    self.collectionView.collectionViewLayout.invalidateLayout()
//}

- (void)configDataWithGuiderCardModel:(GuiderCardModel *)model {
    
    if (model.isfavor) {
        self.collectMarImageView.hidden = NO;
    }else {
        self.collectMarImageView.hidden = YES;
    }
    if (model.certificate.length) {
        self.AuthenticationLabelH.constant = 20;
        self.guiderTagLabel.text = model.certificate;
    }else {
        self.guiderTagLabel.text = @"";
        self.AuthenticationLabelH.constant = 0;
    }
    self.guiderHeaderView.headerImageName = model.headimg;
    self.nameLabel.text = model.realname;
    if (model.introduction.length > 0) {
        self.introduceLabel.attributedText = [IGUtils attributedStringText:model.introduction font:Font(10) color:kContentColor lineSpacing:lineSpace];;
    }else {
        self.introduceLabel.text = @"";
    }
    
    self.guiderHeaderView.vip = model.vip;
    
    self.viewCountLabel.text =[NSString stringWithFormat:@"%@",model.viewcount];
    self.visitCountLabel.text = [NSString stringWithFormat:@"%@",model.visitcount];
    self.favourCountLabel.text = [NSString stringWithFormat:@"%@",model.favourcount];
    
    [self.dataSource removeAllObjects];
    self.dataSource = [NSMutableArray arrayWithArray:model.lines];
    
    [self.ScenicCollectionView.collectionViewLayout invalidateLayout];
    [self.ScenicCollectionView reloadData];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    GuiderCardScenicCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"GuiderCardScenicCell" forIndexPath:indexPath];
    
    LineModel *model = self.dataSource[indexPath.row];
    
    [cell configDataWithModel:model];
    
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CGFloat width =  SCREEN_WIDTH - 100;
    
    if (self.dataSource.count == 1) {
        return CGSizeMake((width - 30), (width - 30));
    }else if(self.dataSource.count == 2) {
        return CGSizeMake(width - 30, (width - 30)/2);
    }
    
    return CGSizeMake((width - 35)/2, (width - 35)/2);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    
    return UIEdgeInsetsMake(15, 15, 15, 15);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    
    return 5;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    
    return 5;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    LineModel *model = self.dataSource[indexPath.row];
    
    if ([_delegate respondsToSelector:@selector(selectLine:)]) {
        [_delegate selectLine:model];
    }
    
}

//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
//
//}
@end
